package org.jeecg.modules.jxc.goodstype.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.checkerframework.checker.units.qual.A;
import org.jeecg.modules.jxc.goodstype.entity.Goods;
import org.jeecg.modules.jxc.goodstype.entity.JxcGoodsType;
import org.jeecg.modules.jxc.goodstype.mapper.GoodsMapper;
import org.jeecg.modules.jxc.goodstype.mapper.JxcGoodsTypeMapper;
import org.jeecg.modules.jxc.goodstype.service.GoodsService;
import org.jeecg.modules.jxc.homepage.entity.JxcGoods;
import org.jeecg.modules.jxc.homepage.mapper.JxcGoodsMapper;
import org.jeecg.modules.jxc.homepage.service.impl.JxcGoodsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @company: yaorange
 * @author: Mr.Yu
 * @version: 1.0
 * @create: 2021-12-22 10:22:21
 */

@Service
public class GoodsServiceImpl implements GoodsService {

    private static final Integer GOODS_TYPE_STATE=0;

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private JxcGoodsTypeMapper jxcGoodsTypeMapper;

    @Override
    @Transactional
    public List<Goods> selectList(String typeId,String good) {
        //判断是否为所有类别
        if (Integer.parseInt(typeId)!=1){
            QueryWrapper<JxcGoodsType> wrapper = new QueryWrapper<>();
            wrapper.eq("goods_type_id",typeId);
            JxcGoodsType jxcGoodsType = jxcGoodsTypeMapper.selectOne(wrapper);
            //判断是否为最后节点
            if (Integer.parseInt(jxcGoodsType.getGoodsTypeState())==GOODS_TYPE_STATE){
                List<Goods> goods = goodsMapper.selectList(typeId, good);
                return goods;

            }else {
                QueryWrapper<JxcGoodsType> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("p_id",typeId);
                List<JxcGoodsType> list = jxcGoodsTypeMapper.selectList(queryWrapper);
                ArrayList<Goods> arrayList = new ArrayList<>();
                for (int i = 0; i < list.size(); i++) {
                    String id = list.get(i).getGoodsTypeId();
                    List<Goods> goods = goodsMapper.selectList(id, good);
                    for (int j = 0; j < goods.size(); j++) {
                        arrayList.add(goods.get(j));
                    }
                }
                return arrayList;
            }
        }
        List<Goods> goods = goodsMapper.selectList(null, good);
        return goods;
    }

    @Override
    public List<JxcGoodsType> selectTypeList() {
        QueryWrapper<JxcGoodsType> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_type_state",GOODS_TYPE_STATE);
        List<JxcGoodsType> list = jxcGoodsTypeMapper.selectList(wrapper);
        return list;
    }

    @Override
    @Transactional
    public void add(Goods goods) {
        goods.setDelFlag(0);
        goods.setState(0);
        goods.setInventoryQuantity(goods.getMinNum());
        goods.setLasjxcPurchasingPrice(goods.getPurchasingPrice());
        goodsMapper.insert(goods);
        System.out.println(goods);
    }

    @Override
    public Goods selectById(String id) {

        Goods goods = goodsMapper.selectById(id);
        return goods;
    }

    @Override
    @Transactional
    public void updateById(Goods goods) {

        goodsMapper.updateById(goods);
    }

    @Override
    public void deleteById(String id) {
        goodsMapper.deleteById(id);
    }
}
